
# coding: utf-8

# In[1]:


from urllib import request,parse
import json
import time
import random
import hashlib


# In[4]:


url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"

keyword = 'python'
#定义请求参数
salt = str(int(time.time() * 1000) + random.randint(0, 10))
# sign是由四部分内容然后计算md5值算出来的
sign = "fanyideskweb" + keyword + salt + "6x(ZHw]mwzX#u0V7@yfwK"
m = hashlib.md5()
m.update(sign.encode('utf-8'))

#定义POST提交的数据

data = {
    'i': keyword,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': salt,
    'sign': m.hexdigest(),
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTIME',
    'typoResult': 'false',
}
data = parse.urlencode(data)



# In[5]:


#'Cookie': 'OUTFOX_SEARCH_USER_ID=-306587578@10.169.0.83; JSESSIONID=aaagZqISzOXO-6ZopNErw; OUTFOX_SEARCH_USER_ID_NCOO=1090114024.6741445; ___rl__test__cookies=1530596919060',
headers = {
        'Cookie': 'OUTFOX_SEARCH_USER_ID=-1984822116@10.168.8.63; OUTFOX_SEARCH_USER_ID_NCOO=1342120865.1664996; fanyi-ad-id=49843; fanyi-ad-closed=1; JSESSIONID=aaaQbWVqvXc_F8LPUWaxw; ___rl__test__cookies=1536537340332',
         'Referer': 'http://fanyi.youdao.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    }
req = request.Request(url,data=bytes(data,encoding="utf-8"),headers=headers)
res = request.urlopen(req)

str_json = res.read().decode("utf-8")
myjson = json.loads(str_json)

for i in range(len(myjson['smartResult']['entries'])):
    s = myjson['smartResult']['entries'][i]
    s = s.replace("\n", "")
    print(s)

